package com.wyz.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.wyz.common.Constant;
import com.wyz.common.aop.SystemLog;
import com.wyz.mapper.UserMapper;
import com.wyz.model.Contact;
import com.wyz.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 用户页面处理器
 */
@Controller
public class UserController {
    @Resource
    private UserMapper userMapper;

    //登录请求
    @RequestMapping("/user/login.do")
    @SystemLog(moduleName = Constant.LOGIN_MODULE_NAME,desc = Constant.QUERY_DESC)
    public String login(String account, String password, HttpSession session) {
        System.out.println(account);
        System.out.println(password);

        //查询数据库是否存在该用户名和密码
        Map<String,Object> map = new HashMap<>();
        map.put("account",account);
        map.put("password",password);
        List<User> users = userMapper.selectByMap(map);
        if(users.size() == 0) {
            session.setAttribute("error", "用户名或密码错误"); // 登录失败后将错误信息存入session中
            return "back/login";
        } else {
            session.removeAttribute("error"); // 修改成功后清除session中错误信息
            session.setAttribute("account", account); // 将登录账号存入session中
            return "back/index"; // 登录成功后跳转到主页
        }
    }


    //查看个人信息请求
    @RequestMapping("/user/info.do")
    public String info(HttpSession session) {
        //从session中获取用户登录的账号
        String account = (String)session.getAttribute("account");
        System.out.println(account);
        //从数据库查询用户信息
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("account",account));
        //将用户信息保存到session中
        session.setAttribute("user", user);
        return "back/edit_User_Information";
    }

    //修改密码请求
    @RequestMapping("/user/pwd.do")
    public String pwd(String password,HttpSession session) {
        //从 session 中获取用户登录账号
        String account = (String)session.getAttribute("account");
        //更新数据库中用户的密码
        Map<String,Object> map = new HashMap<>();
        map.put("account",account);
        map.put("password",password);
        int result = userMapper.update(new UpdateWrapper<User>().set("password", password).eq("account", account));
        if (result == 0) {
            session.setAttribute("error", "修改失败"); // 修改失败后将错误信息存入session中
            return "password";
        } else {
            session.removeAttribute("error"); // 修改成功后清除session中错误信息
            return  "redirect:/login.jsp"; // 修改成功后重定向跳转到登录页面
        }
    }


    @RequestMapping("/user/getUserByAccount")
   public String getUserByAccount(String account, Model model) {
        User user =userMapper.selectOne(new QueryWrapper<User>().eq("account",account));
        model.addAttribute("user",user);
        return "back/user";
    }

    @RequestMapping("/user/userEdit.do")
    public String contactEditHandler(User user) {
        //数据库修改联系人
        userMapper.updateById(user);
        return "redirect:/back/login.jsp"; // 重定向跳转到联系人页面
    }
    @RequestMapping("/user/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("account"); // 清除session中登录账号
        return "redirect:/back/login.jsp"; // 重定向跳转到登录页面
    }
}












